package com.sun.rabbit.did_queue;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.sun.rabbit.utils.RabbitMqUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**消费者（启动之后关闭该消费者 模拟其接收不到消息）：
 * 目的：消费生产者发过来的消息
 * @author shkstart
 * @create 2021-10-18 20:31
 */
public class Consumer01 {
    //普通交换机的名称
    private final static String EXCHANGE_NAME="normal_exchange";
    //普通对列的名称
    private final static String NORMAL_QUEUE="normal_queue";
    //死信交换机的名称
    private final static String DEAD_EXCHANGE = "dead_exchange";
    //死信对列的名称
    private final static String DEAD_QUEUE="dead_queue";

    public static void main(String[] args) throws Exception {
        consumer_03();


    }
//消息被拒
    private static void consumer_03() throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
            channel.exchangeDeclare(DEAD_EXCHANGE,BuiltinExchangeType.DIRECT);
            channel.queueDeclare(DEAD_QUEUE,false,false,false,null);
            channel.queueBind(DEAD_QUEUE,DEAD_EXCHANGE,"lisi");
            Map<String, Object> map = new HashMap<>();
            map.put("x-dead-letter-exchange",DEAD_EXCHANGE);
            map.put("x-dead-letter-routing-key","lisi");
            channel.queueDeclare(NORMAL_QUEUE,false,false,false,map);
            channel.queueBind(NORMAL_QUEUE,EXCHANGE_NAME,"zhangsan");
            System.out.println("等待接受消息。。。");
            DeliverCallback deliverCallback=(consumerTag, delivery)->{
                String message = new String(delivery.getBody(), "UTF-8");
                if(message.equals("info5")){
                    System.out.println("Consumer01 接收到消息" + message +" 并拒绝签收该消息");
                    //requeue 设置为 false 代表拒绝重新入队 该队列如果配置了死信交换机将发送到死信队列中
                    channel.basicReject(delivery.getEnvelope().getDeliveryTag(),false);
                }else{
                    System.out.println("Consumer01 接收到消息"+message);
                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
                }
            };
            channel.basicConsume(NORMAL_QUEUE,false,deliverCallback,consumerTag -> {});

    }

    //队列达到最大长度(注意此时需要把原先队列删除 因为参数改变了)
    private static void consumer_02() throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        //声明普通交换机和死信交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        channel.exchangeDeclare(DEAD_EXCHANGE,BuiltinExchangeType.DIRECT);
        //声明死信对列
        channel.queueDeclare(DEAD_QUEUE,false,false,false,null);
        //死信对列绑定死信交换机
        channel.queueBind(DEAD_QUEUE,DEAD_EXCHANGE,"lisi");

        // 正常队列绑定死信队列信息
        HashMap<String, Object> map = new HashMap<>();
        //正常对列设置死信交换机 参数是固定值
        map.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        //正常队列设置死信死信路由键 routing-key 参数 key 是固定值
        map.put("x-dead-letter-routing-key","lisi");
        //设置普通对列的长度限制
        map.put("x-max-length",6);
        //声明普通对列
        channel.queueDeclare(NORMAL_QUEUE,false,false,false,map);
        //普通对列绑定普通交换机
        channel.queueBind(NORMAL_QUEUE,EXCHANGE_NAME,"zhangsan");
        System.out.println("等待接受消息。。。。");

        DeliverCallback deliverCallback=(consumerTag, delivery)->{
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println("消费者c1接受消息:"+message);
        };
        channel.basicConsume(NORMAL_QUEUE,true,deliverCallback,consumerTag ->{System.out.println("消息中断。。。");});
    }
    private static void consumer_01() throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        //声明普通交换机和死信交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        channel.exchangeDeclare(DEAD_EXCHANGE,BuiltinExchangeType.DIRECT);
        //声明死信对列
        channel.queueDeclare(DEAD_QUEUE,false,false,false,null);
        //死信对列绑定死信交换机
        channel.queueBind(DEAD_QUEUE,DEAD_EXCHANGE,"lisi");

        // 正常队列绑定死信队列信息
        HashMap<String, Object> map = new HashMap<>();
        //正常对列设置死信交换机 参数是固定值
        map.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        //正常队列设置死信死信路由键 routing-key 参数 key 是固定值
        map.put("x-dead-letter-routing-key","lisi");

        //声明普通对列
        channel.queueDeclare(NORMAL_QUEUE,false,false,false,map);
        //普通对列绑定普通交换机
        channel.queueBind(NORMAL_QUEUE,EXCHANGE_NAME,"zhangsan");
        System.out.println("等待接受消息。。。。");

        DeliverCallback deliverCallback=(consumerTag, delivery)->{
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println("消费者c1接受消息:"+message);
        };
        channel.basicConsume(NORMAL_QUEUE,true,deliverCallback,consumerTag ->{System.out.println("消息中断。。。");});
    }

}
